package com.xxl.job.executor.sample.frameless.test.MongoDB;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.ArrayList;

public class MongoDB1 {
    public static void main(String[] args) {
        //1、MongoDB连接
        MongoClient mongoClient = new MongoClient("127.0.0.1",27017);
        //2、获取一个数据库，不存在会自动创建
        MongoDatabase mongoDatabase = mongoClient.getDatabase("java");
        //3、获取集合，不存在会自动创建
        MongoCollection<Document> java_collection = mongoDatabase.getCollection("java_collection");
        //4、单条增加
        Document document1 = new Document();
        document1.append("name","三国演义").append("author","罗贯中");
        java_collection.insertOne(document1);
        //5、添加多条
        ArrayList<Document> documentArrayList = new ArrayList<>();
        Document document2 = new Document();
        document2.append("name","西游记").append("author","吴承恩");
        Document document3 = new Document();
        document3.append("name","红楼梦").append("author","曹雪芹");
        documentArrayList.add(document2);
        documentArrayList.add(document3);
        java_collection.insertMany(documentArrayList);
    }
}
